package cn.edu.sgu.www.mhxysy.service.chongwu_shizhuang.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.chongwu_shizhuang.ChongwuShizhuangAttributeDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu_shizhuang.ChongwuShizhuangAttribute;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_shizhuang.ChongwuShizhuangAttributeMapper;
import cn.edu.sgu.www.mhxysy.service.chongwu_shizhuang.ChongwuShizhuangAttributeService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import cn.edu.sgu.www.mhxysy.vo.except.AttributeVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuShizhuangAttributeServiceImpl implements ChongwuShizhuangAttributeService {

    private final ChongwuShizhuangAttributeMapper chongwuShizhuangAttributeMapper;

    @Autowired
    public ChongwuShizhuangAttributeServiceImpl(ChongwuShizhuangAttributeMapper chongwuShizhuangAttributeMapper) {
        this.chongwuShizhuangAttributeMapper = chongwuShizhuangAttributeMapper;
    }

    @Override
    public void insert(ChongwuShizhuangAttributeDTO chongwuShizhuangAttributeDTO) {
        ChongwuShizhuangAttribute chongwuShizhuangAttribute = toEntity(chongwuShizhuangAttributeDTO);

        chongwuShizhuangAttribute.setId(chongwuShizhuangAttribute.uuid());

        chongwuShizhuangAttributeMapper.insert(chongwuShizhuangAttribute);
    }

    @Override
    public void deleteById(String id) {
        chongwuShizhuangAttributeMapper.deleteById(id);
    }

    @Override
    public void updateById(ChongwuShizhuangAttributeDTO chongwuShizhuangAttributeDTO) {
        ObjectUtils.requireNonNull(chongwuShizhuangAttributeDTO.getId(), CustomMessages.ID_NOT_NULL);

        ChongwuShizhuangAttribute chongwuShizhuangAttribute = toEntity(chongwuShizhuangAttributeDTO);

        chongwuShizhuangAttributeMapper.updateById(chongwuShizhuangAttribute);
    }

    @Override
    public AttributeVO selectAttribute(String shizhuangId) {
        return chongwuShizhuangAttributeMapper.selectAttribute(shizhuangId);
    }

    @Override
    public List<ChongwuShizhuangAttribute> selectByShizhuangId(String shizhuangId) {
        if (StringUtils.isNullOrEmpty(shizhuangId)) {
            return new ArrayList<>();
        }

        QueryWrapper<ChongwuShizhuangAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("shizhuang_id", shizhuangId);

        return chongwuShizhuangAttributeMapper.selectList(queryWrapper);
    }

    private ChongwuShizhuangAttribute toEntity(ChongwuShizhuangAttributeDTO chongwuShizhuangAttributeDTO) {
        ChongwuShizhuangAttribute chongwuShizhuangAttribute = new ChongwuShizhuangAttribute();

        chongwuShizhuangAttribute.setId(chongwuShizhuangAttributeDTO.getId());
        chongwuShizhuangAttribute.setPropertyId(chongwuShizhuangAttributeDTO.getPropertyId());
        chongwuShizhuangAttribute.setShizhuangId(chongwuShizhuangAttributeDTO.getShizhuangId());
        chongwuShizhuangAttribute.setPropertyValue(chongwuShizhuangAttributeDTO.getPropertyValue());

        return chongwuShizhuangAttribute;
    }

}